/*
 * global.java
 *
 * Created on March 10, 2006, 7:00 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author Sujal Bista
 */

import javax.swing.JComboBox;
import java.util.Vector;
import java.sql.*;

public class globalData
{
    //content of the setup file reader
    public static readSetupFile setupInfo;
    
    //current judging system
    // 0 Traditional System
    // 1 Nandu system with AB judges (for compulsory)
    // 2 Nandu System with ABC judges
    public static int scoringSystem;
       
    public static JComboBox athleteList;
    public static int eventCount;
    //public static event events[];
    public static String currentAge;
    public static String currentGroup;
    public static String currentGender;
    public static event currentEvent;
    public static ResultSet currentAthleteSQL;
    //keeps the boolean value to see if the judge is connected
    //first 3 for judge A
    //second 3 for judge B
    //last 3 for judge C
    public static boolean judgesConnected[];
    
    public static judge judges[];
    public static serveClient clients[];
    
       
    public static long startTime;
    public static long endTime;
    public static long lastTime;
    public static long diffTime;
    
    //tracking if any athlete is currently active
    public static boolean athleteActive;
    public static boolean athleteScorable;
    public static athlete currentAthlete;
    
    //total number of judges allowed
    public static int totalJudges;
    
    //enables 3 second timer rule for judge 
    //note : by setting this value to really high, one can disable judge a timer constraint.
    public static int JudgeATimeRangeInSec;
    
    public static boolean displayScore; //shows the calculated score
    public static boolean enableModifyScore; //allows head judge to modify score
    public static String titleName; //title of the program
    
    public static semaphore mutex;
    
    public static nanduData nanduList;
    
    // group gender information
    //names are filled by reading the setup file
    public static String groupInfo[];
    
    //list of events
    //names are filled by reading the setup file
    public static String eventInfo[];

    //list of age group level and gender allowed in the tournament
    public static String AgeGroupList[];
    public static String LevelList[];
    public static String GenderList[];
    
    public static class ScoringSystem
    {
        public String eventName;   //name of event same as eventInfo
        public String level;   // Skill level Beg Int Adv
        public String scoringType ;//TraditionalSystem,CompulsorySystem,NanduSystem

        public ScoringSystem(String eventName, String level, String scoringType)
        {
            this.eventName=eventName;   //name of event same as eventInfo
            this.level=level;   // Skill level Beg Int Adv
            this.scoringType=scoringType;//Tradional vs Nandu
        }
        
    }
      
    //used to determine which scoring system to use
    public static ScoringSystem scoringSystemInfo[];
    
    //list of events used to calculate barehand score
    public static Vector BareHandEventName;
    
    //flag to tell that user has been added
    public static boolean userAddedFlag;
    
    public static Vector teamSelectionEventList0;
    public static Vector teamSelectionEventList1;
    
    public static void initGlobalData()
    {
        scoringSystem=2;  //turn this on to enable traditional judging
        totalJudges=10;                 //total number of judges
        judgesConnected=new boolean[totalJudges];

        nanduList=new nanduData();
                
        judges=new judge[totalJudges];
        clients=new serveClient[totalJudges];
        
        String jSign="";
        
        globalData.judges[0]=new judge(judge.judgeType.judgeA,jSign);
        globalData.judges[1]=new judge(judge.judgeType.judgeA,jSign);
        globalData.judges[2]=new judge(judge.judgeType.judgeA,jSign);
        
        globalData.judges[3]=new judge(judge.judgeType.judgeB,jSign);
        globalData.judges[4]=new judge(judge.judgeType.judgeB,jSign);
        globalData.judges[5]=new judge(judge.judgeType.judgeB,jSign);
        
        globalData.judges[6]=new judge(judge.judgeType.judgeC,jSign);
        globalData.judges[7]=new judge(judge.judgeType.judgeC,jSign);
        globalData.judges[8]=new judge(judge.judgeType.judgeC,jSign);
        
        globalData.judges[9]=new judge(judge.judgeType.judgeB,jSign);
                
        for(int i=0;i<totalJudges-1;i++)
        {
            judgesConnected[i]=false;
        }
        judgesConnected[totalJudges-1]=true;
        
        eventCount=0;
        currentGroup = "";
        currentGender = "";
        currentAge = "";
        athleteActive=false;
        athleteScorable = false;
        mutex=new semaphore(1);
        userAddedFlag=true;
        
        //enables 3 second timer rule for judge 
        //note : by setting this value to really high, one can disable judge a timer constraint.
        JudgeATimeRangeInSec=3;
        
        displayScore=true; //shows the calculated score
        enableModifyScore=true; //allows head judge to modify score
        titleName=new String("Wushu Tournament");
        
        //used for team selection
        teamSelectionEventList0=new Vector();
        teamSelectionEventList1=new Vector();
        
        //read setup file first
        setupInfo=new readSetupFile();
    }
    
    public static long getTime()
    {
         long currTime = System.currentTimeMillis();
         if (lastTime < startTime) {
             lastTime = startTime;
         }
         long newdiff = currTime - lastTime;

            //round off
         //newdiff/=1000;
         //newdiff*=1000;
            
         diffTime += newdiff;
         lastTime = currTime;
         
         return diffTime;
    }
    
    //returns free judge
    public static synchronized int getFreeJudge(judge.judgeType jType)
    {
        int retVal=-1;
        mutex.acquire();
        /*
         if(traditionalScoringSystem==true)
        {
            for(int i=0;i<totalJudges-1;i++)
            {
                if(judgesConnected[i]==false)
                {
                    judgesConnected[i]=true;
                    retVal=i;
                    break;
                }
            }        
        }
        else
         */
        {
            if(jType==judge.judgeType.judgeA)
            {
                for(int i=0;i<3;i++)
                {
                    if(judgesConnected[i]==false)
                    {
                        judgesConnected[i]=true;
                        retVal=i;
                        break;
                    }
                }
            }else if(jType==judge.judgeType.judgeB)
            {
                for(int i=3;i<6;i++)
                {
                    if(judgesConnected[i]==false)
                    {
                        judgesConnected[i]=true;
                        retVal=i;
                        break;
                    }
                }
            }else if(jType==judge.judgeType.judgeC)
            {
                for(int i=6;i<(globalData.totalJudges-1);i++)
                {
                    if(judgesConnected[i]==false)
                    {
                        judgesConnected[i]=true;
                        retVal=i;
                        break;
                    }
                }
            }
        }
        mutex.release();
        return retVal;
    }
    
    public static synchronized boolean isJudgeConnected(int index)
    {
        mutex.acquire();
        boolean value=judgesConnected[index];
        mutex.release();
        return value;
    }
    
    public static synchronized void freeJudge(int index)
    {
        mutex.acquire();
        if(index<(globalData.totalJudges-1))
        {
            judgesConnected[index]=false;
        }
        mutex.release();
    }
    
    public static synchronized void setUserAdded(boolean value)
    {
        userAddedFlag=value;
    }
    
    public static synchronized boolean getUserAdded()
    {
        return userAddedFlag;
    }
   
    //converts judges index to appopriate type
    public static judge.judgeType judgeIndexToJudgeType(int judgeIndex)
    {
        if(globalData.scoringSystem==0)
            return judge.judgeType.judgeB;   //judge b
        else if(globalData.scoringSystem==1)
        {
            if(judgeIndex==0 || judgeIndex==1 || judgeIndex==2 || judgeIndex==6 || judgeIndex==7)
                return judge.judgeType.judgeA; //judge A
            else
                return judge.judgeType.judgeB;//judge B
        }
        else
        {
            if(judgeIndex==0 || judgeIndex==1 || judgeIndex==2)
                return judge.judgeType.judgeA; //judge A
            else if(judgeIndex==6 || judgeIndex==7 || judgeIndex==8)
                return judge.judgeType.judgeC;//judge C
            return judge.judgeType.judgeB; //judge B
        }
    }
    
    public static String getSetupInfo()
    {
        String data=new String();
        data+=JudgeATimeRangeInSec;
        return data;
    }
    
    public static void setLock()
    {
        mutex.acquire();
    }
    
    public static void releaseLock()
    {
        mutex.release();
    }
}
